package com.baizhi.llw.service;

import com.baizhi.llw.dao.CategoryMapper;
import com.baizhi.llw.dto.CommonPageDTO;
import com.baizhi.llw.entity.Category;
import com.baizhi.llw.entity.CategoryExample;
import com.baizhi.llw.entity.User;
import com.baizhi.llw.vo.CategoryPageVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CategoryPageVO queryOnePage(Integer page, Integer pageSize) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);
        List<Category> list =categoryMapper.selectByExampleAndRowBounds(categoryExample,new RowBounds((page-1)*pageSize,pageSize));
        int total = categoryMapper.selectCountByExample(categoryExample);
        return new CategoryPageVO(page,total,list);
    }

    @Override
    public CategoryPageVO queryTwoPage(Integer page, Integer pageSize,Integer categoryId) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(categoryId);
        List<Category> list = categoryMapper.selectByExampleAndRowBounds(categoryExample, new RowBounds((page - 1) * pageSize, pageSize));
        int total = categoryMapper.selectCountByExample(categoryExample);
        return new CategoryPageVO(page,total,list);
    }

    @Override
    public void queryAdd(Category c) {
        c.setLevels(1);
        categoryMapper.insert(c);
    }

    @Override
    public void queryRemove(Category c) {
        CategoryExample example = new CategoryExample();
        example.createCriteria().andIdEqualTo(c.getId());
        //删除数据
        categoryMapper.deleteByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Category queryOne(int id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public void queryUpdate(Category c) {
        categoryMapper.updateByPrimaryKeySelective(c);
    }
}
